<template>
    <cl-crud ref="Crud">
        <cl-row>
            <!-- 刷新按钮 -->
            <cl-refresh-btn />
            <!-- 新增按钮 -->
            <!-- <cl-add-btn /> -->
            <!-- 删除按钮 -->
            <cl-multi-delete-btn />
            <cl-filter label="记录保存天数">
                <el-input-number v-model="day" controls-position="right" :max="10000" :min="1" @change="saveDay" />
            </cl-filter>
            <cl-flex1 />
            <!-- 关键字搜索 -->
            <cl-search-key :placeholder="'搜索用户名、用户id、象数'" />

        </cl-row>

        <cl-row>
            <!-- 数据表格 -->
            <cl-table ref="Table" />
        </cl-row>

        <cl-row>
            <cl-flex1 />
            <!-- 分页控件 -->
            <cl-pagination />
        </cl-row>

        <!-- 新增、编辑 -->
        <cl-upsert ref="Upsert" />
    </cl-crud>
</template>

<script lang="ts" name=" broadcast-record" setup>
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";

const { service } = useCool();

// 天数
const day = ref(1);

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "用户ID",
			prop: "userId",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "配方ID",
			prop: "recipeId",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "象数",
			prop: "elephantNumber",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		}
	]
});
// 保存天数
function saveDay() {
	service.broadcast.record
		.setKeep({ value: day.value })
		.then(() => {
			ElMessage.success("保存成功");
		})
		.catch((err) => {
			ElMessage.error(err.message);
		});
}
onMounted(() => {
	// 获取天数
	service.broadcast.record.getKeep().then((res) => {
		day.value = Number(res);
	});
});
// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "用户ID", prop: "userId" },
		{ label: "用户昵称", prop: "nickName", minWidth: 140 },
		{ label: "用户头像", prop: "avatarUrl", minWidth: 140, component: { name: "cl-avatar" } },
		{ label: "配方名称", prop: "name", minWidth: 140 },
		{ label: "背景音频", prop: "audioName", minWidth: 140 },
		{ label: "象数", prop: "elephantNumber", minWidth: 140 },
		{ label: "播放次数", prop: "count", minWidth: 140 },
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 160,
			component: { name: "cl-date-text" }
		},
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 160,
			sortable: "desc",
			component: { name: "cl-date-text" }
		}
		// { type: "op", buttons: ["delete"] } //"edit",
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.broadcast.record
	},
	(app) => {
		app.refresh();
	}
);
</script>
